//
//  UIView+Responser.m
//  ResponserChain
//
//  Created by 苏沫离 on 2018/11/21.
//  Copyright © 2018 苏沫离. All rights reserved.
//

#import "UIView+Responser.h"
#import <objc/runtime.h>

#define NSLog(FORMAT, ...)  fprintf(stderr,"%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);


@implementation UIView (Responser)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        Class class = [self class];
        
        Method pointMethodOld = class_getInstanceMethod(class, @selector(pointInside:withEvent:));
        Method pointMethod = class_getInstanceMethod(class, @selector(l_pointInside:withEvent:));
        BOOL pointAdd = class_addMethod(class, @selector(pointInside:withEvent:), method_getImplementation(pointMethod), method_getTypeEncoding(pointMethod));
        if (pointAdd) {
            class_replaceMethod(class, @selector(l_pointInside:withEvent:), method_getImplementation(pointMethodOld), method_getTypeEncoding(pointMethodOld));
        }else{
            method_exchangeImplementations(pointMethodOld, pointMethod);
        }
        
        
        Method hitMethodOld = class_getInstanceMethod(class, @selector(hitTest:withEvent:));
        Method hitMethod = class_getInstanceMethod(class, @selector(l_hitTest:withEvent:));
        BOOL hitAdd = class_addMethod(class, @selector(hitTest:withEvent:), method_getImplementation(hitMethod), method_getTypeEncoding(hitMethod));
        if (hitAdd) {
            class_replaceMethod(class, @selector(l_hitTest:withEvent:), method_getImplementation(hitMethodOld), method_getTypeEncoding(hitMethodOld));
        }else{
            method_exchangeImplementations(hitMethodOld, hitMethod);
        }
        
        
        
    });
}



// 检测坐标点是否落在当前视图范围内
- (BOOL)l_pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
//    NSLog(@"当前视图 pointInside ======== %@ : %ld",self.class,self.tag);
    BOOL pointInside = [self l_pointInside:point withEvent:event];
//    if (pointInside) {
//        NSLog(@"当前视图范围内 ====== %@ : %ld ",self.class,self.tag);
//    }else{
//        NSLog(@"当前视图范围外 ====== %@ : %ld ",self.class,self.tag);
//    }

    return pointInside;
}

//查找响应处理事件的最终视图
- (UIView *)l_hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
//    NSLog(@"当前视图     hitTest -------- %@ : %ld",self.class,self.tag);
    UIView *responserView = [self l_hitTest:point withEvent:event];
//    if (responserView) {
//        NSLog(@"当前视图 %@ : %ld -- 响应视图 %@ : %ld ",self.class,self.tag,responserView.class,(long)responserView.tag);
//    }else{
//         NSLog(@"当前视图 %@ : %ld -- 响应视图 查找不到",self.class,self.tag);
//    }
    return responserView;
}


- (void)touchesBegan: (NSSet<UITouch *> *)touches withEvent: (UIEvent *)event {
//    [super touchesBegan:touches withEvent:event];
    NSLog(@"began --- %ld",(long)self.tag);
}

- (void)touchesEnded: (NSSet<UITouch *> *)touches withEvent: (UIEvent *)event {
    NSLog(@"ended ---- %ld",(long)self.tag);
}



- (UIViewController *)viewController
{
    UIResponder *nextVC = self.nextResponder;
    
    while ([nextVC isKindOfClass:UIViewController.class] == NO)
        nextVC = nextVC.nextResponder;
    
    return [nextVC isKindOfClass:UIViewController.class] ? nextVC : nil;
}

@end
